package wenjun.du.androidart.animator

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Choreographer
import kotlinx.android.synthetic.main.activity_animator_use.*
import wenjun.du.androidart.R

/**
 * view动画和帧动画
 *
 * 属性动画的基本上使用
 *
 * */
class AnimatorUseActivity : AppCompatActivity() {
    companion object {
        private const val TAG = "AnimatorUseActivity"
    }

    private val mChoreographer: Choreographer = Choreographer.getInstance()

    private val mFrameCallback: Choreographer.FrameCallback = MyFrameCallback()

    var count: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_animator_use)
        choreographer.setOnClickListener {
            mChoreographer.postFrameCallback(mFrameCallback)
        }

        valueAnimator_user.setOnClickListener {
            //valueAnimator的基本使用
            val ofInt = ValueAnimator.ofInt(0, 100)
            ofInt.duration = 1000
            ofInt.addUpdateListener {
                Log.d(TAG, "onCreate() called 更新进度 ======  ${it.animatedFraction} count ${count++}")
            }
            ofInt.start()
        }

        objectAnimator_user.setOnClickListener {
            ObjectAnimator.ofFloat(target_animator, "TranslationX", 150f, 250f).setDuration(1000).start()
        }
    }


    inner class MyFrameCallback : Choreographer.FrameCallback {
        override fun doFrame(frameTimeNanos: Long) {
            Log.d(
                TAG,
                "doFrame() called with: frameTimeNanos = ${frameTimeNanos / 1000000}"
            )
            //垂直信号回调
            mChoreographer.postFrameCallback(mFrameCallback)
        }
    }
}